import getInstanceProps from "@/instance";

const instanceProps = getInstanceProps();

export class PropsWatcher {
  constructor(vm, callback) {
    this.callback = callback;

    Object.keys(vm).forEach((prop) => {
      let value = vm[prop];
      Object.defineProperty(vm, prop, {
        get: () => value,
        set: (newValue) => {
          const oldValue = value;
          if (oldValue !== newValue) { // important to avoid the program render max size
            value = newValue;

            if (!instanceProps.includes(prop)) {
              // when instanceProps not includes , should callback to customer.
              this.callback([newValue, oldValue], prop);
            }
          }
        }
      });
    });
  }
}
